﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HumanManagement.BusinessLogic;
using HumanManagement.BusinessObjects;
using HumanManagement.DataAccess;
using System.Data;

namespace BSC_HumanManagement.Usercontrols
{
    public partial class uc_HopDongLaoDong_Detail : System.Web.UI.UserControl
    {
        int itemID = 0;
        Contract objContract = new Contract();
        ContractBL objContractBL = new ContractBL();
        //public bool permissionAddNew = false;

        protected void Page_Load(object sender, EventArgs e)
        {
            this.txtEffectiveDate.MaxDate = DateTime.MaxValue;
            this.txtEffectiveDate.MinDate = DateTime.MinValue;
            this.txtExpireDate.MaxDate = DateTime.MaxValue;
            this.txtExpireDate.MinDate = DateTime.MinValue;
            PersonalProfileBL _objPersonalProfileBL = new PersonalProfileBL();
            foreach (DataRow item in _objPersonalProfileBL.GetDataSet(Convert.ToInt32(Session["PersonalProfileID"].ToString())).Tables[0].Rows)
            {
                this.ddlPersonalProfileID.Items.Add(new ListItem(item["FullName"].ToString(), item["ID"].ToString()));
            }
            ContractTypeBL _objContractTypeBL = new ContractTypeBL();
            foreach (DataRow item in _objContractTypeBL.GetDataSet().Tables[0].Rows)
            {
                this.ddlContractTypeID.Items.Add(new ListItem(item["ContractType"].ToString(), item["ID"].ToString()));
            }
            if (Request.QueryString["ID"] != null && Request.QueryString["FunctionName"] == "Hợp đồng lao động Detail")
            {
                LoadData();
                DisableControl();
                ShowData(objContract);
            }
            if (Request.QueryString["ID"] != null && Request.QueryString["FunctionName"] == "Hợp đồng lao động Edit")
            {
                if (LoadData())
                {
                    ShowData(objContract);
                    this.btnDelete.Visible = false;
                }
                else
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Không tồn tại hồ sơ nhân viên mã ID: " + Request.QueryString["ID"] +
                        " hoặc không có quyền thao tác trên hồ sơ nhân viên này.' )", true);
                    return;
                }
            }
            if (Request.QueryString["ID"] != null && Request.QueryString["FunctionName"] == "Hợp đồng lao động Delete")
            {
                if (LoadData())
                {
                    ShowData(objContract);
                    this.btnSave.Visible = false;
                }
                else
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Không tồn tại hợp đồng mã ID: " + Request.QueryString["ID"] +
                        " hoặc không có quyền thao tác trên hồ sơ nhân viên này.' )", true);
                    return;
                }
            }
        }

        private void ShowData(Contract _objContract)
        {
            this.txtContractNumber.Text = _objContract.ContractNumber;
            for (int i = 0; i < this.ddlPersonalProfileID.Items.Count; i++)
			{
                if (this.ddlPersonalProfileID.Items[i].Value.Equals(_objContract.PersonalProfileID.ToString()))
                {
                    this.ddlPersonalProfileID.SelectedIndex = i;
                }
			}
            for (int i = 0; i < this.ddlContractTypeID.Items.Count; i++)
            {
                if (this.ddlContractTypeID.Items[i].Value.Equals(_objContract.ContractTypeID.ToString()))
                {
                    this.ddlContractTypeID.SelectedIndex = i;
                }
            }
            this.txtEffectiveDate.SelectedDate = _objContract.EffectiveDate;
            this.txtExpireDate.SelectedDate = _objContract.ExpireDate;
            for (int i = 0; i < this.ddlContractStatus.Items.Count; i++)
            {
                if (this.ddlContractStatus.Items[i].Value == _objContract.ContractStatus)
                {
                    this.ddlContractStatus.SelectedIndex = i;
                }
            }
        }

        private void DisableControl()
        {
            this.ddlContractStatus.Enabled = false;
            this.ddlContractTypeID.Enabled = false;
            this.ddlPersonalProfileID.Enabled = false;
            this.txtContractNumber.Enabled = false;
            this.txtEffectiveDate.Enabled = false;
            this.txtExpireDate.Enabled = false;
        }

        private bool LoadData()
        {
            itemID = Convert.ToInt32(Request.QueryString["ID"]);
            objContract = objContractBL.GetByID(itemID, Convert.ToInt32(Session["PersonalProfileID"].ToString()));
            if (objContract == null)
            {
                return false;
            }
            return true;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            this.objContract.ContractNumber = this.txtContractNumber.Text;
            this.objContract.PersonalProfileID = Convert.ToInt32( this.ddlPersonalProfileID.SelectedValue);
            this.objContract.ContractTypeID = Convert.ToInt32(this.ddlContractTypeID.SelectedValue);
            this.objContract.EffectiveDate = (DateTime) this.txtEffectiveDate.SelectedDate;
            this.objContract.ExpireDate = (DateTime)this.txtExpireDate.SelectedDate;
            this.objContract.ContractStatus = this.ddlContractStatus.SelectedValue;
            this.objContract.LastDate = DateTime.Now;

            if (Request.QueryString["ID"] == null)
            {
                objContractBL.Add(objContract);
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Tạo mới thành công')", true);
                Response.Redirect("/Default.aspx?FunctionName=Hợp đồng lao động");
            }

            if (Request.QueryString["FunctionName"] == "Hợp đồng lao động Edit")
            {
                objContractBL.Update(objContract);
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Sửa thành công')", true);
                Response.Redirect("/Default.aspx?FunctionName=Hợp đồng lao động");
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string confirmValue = Request.Form["confirm_value"];
            if (confirmValue == "Yes")
            {
                objContract = objContractBL.GetByID(Convert.ToInt32(Request.QueryString["ID"]), Convert.ToInt32(Session["PersonalProfileID"].ToString()));
                objContract.ContractStatus = "0";
                objContractBL.Update(objContract);
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Xóa thành công')", true);
                Response.Redirect("/Default.aspx?FunctionName=Hợp đồng lao động");
            }
            else { }
        }
    }
}